<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>vue3-003动态组件</title>
    <script src="./vue3.js"></script>
</head>

<body>
    <div id="app">
        <button @click="isChange = !isChange">切换</button>
        <component :is="isChange ? Comp01 : Comp02 " />
    </div>
    <script>
        const { createApp, defineComponent, ref, onMounted, onUpdated } = Vue

        const Comp01 = defineComponent({
            name: 'Comp01',
            template: `
                <div style='color: red;'>我是组件1</div>
            `,
            unmounted() {
                console.log(1)
            }
        })
        const Comp02 = defineComponent({
            name: 'Comp02',
            template: `
                <div style='color: green;'>我是组件2</div>
            `,
            unmounted() {
                console.log(2)
            }
        })

        createApp({
            setup() {
                const isChange = ref(true)

                return {
                    Comp01, Comp02, isChange
                }
            }
        }).mount('#app')
    </script>
</body>

</html>